﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Linq;
using System.Collections.Generic;
using Baosight.Platform.Core.UI.Controls;
using Baosight.ZNH.ZMCA.UI.Utils;
using Telerik.Windows.Controls.Charting;
using Telerik.Windows.Controls;
using Baosight.Platform.Core;
using Baosight.Platform.Core.ServiceModel;
using Baosight.ZNH.ZMCA.UI.Controls;
using System.Reflection;
using Baosight.ZNH.ZMCA.UI.Model;



namespace Baosight.ZNH.ZMCA.UI
{


    public partial class ZMCA71 : EFView
    {
        ZMCA12Controler controler = new ZMCA12Controler();
        public ZMCA71()
        {
            InitializeComponent();
            FillPieChart();
            FillChartData(this.chartLevel, this.chartClass);
            FillListViewData();

        }

        private void FillListViewData()
        {
            controler.GetListViewData(lstView);
        }


        public void FillChartData(RadChart chartLevel, RadChart chartClass)
        {
            controler.GetRiskChartData(chartLevel, chartClass);
        }

        public void FillPieChart()
        {
            GetPieChartData(PieChart, this.indexTitle);
        }

        private void InitPieChart()
        {
            //分布图
            Common.ClearChart(PieChart);
            PieChart.DefaultView.ChartLegend.Header = "";
            PieChart.DefaultView.ChartLegend.FontSize = 10;
            PieChart.DefaultView.ChartLegend.MaxWidth = 100;
            PieChart.DefaultView.ChartLegendPosition = Telerik.Windows.Controls.Dock.Right;
            SeriesMapping smPie = new SeriesMapping();
            PieSeriesDefinition totalPie = new PieSeriesDefinition();
            smPie.SeriesDefinition = totalPie;
            //sm2.LegendLabel = "单位";
            smPie.ItemMappings.Add(new ItemMapping("XCategory", DataPointMember.XCategory));
            smPie.ItemMappings.Add(new ItemMapping("YValue", DataPointMember.YValue));
            smPie.ItemMappings.Add(new ItemMapping("LegendLabel", DataPointMember.LegendLabel));

            totalPie.InteractivitySettings.HoverScope = InteractivityScope.Item;
            totalPie.InteractivitySettings.SelectionScope = InteractivityScope.Item;
            totalPie.InteractivitySettings.SelectionMode = ChartSelectionMode.Single;
            totalPie.LegendDisplayMode = LegendDisplayMode.DataPointLabel;

            PieChart.SeriesMappings.Add(smPie);
        }

        public void GetPieChartData(RadChart PieChart, TextBlock text)
        {
            this.PieChart = PieChart;
            text.Text = "昨日隐患对比";

            EIInfo eiInfo = new EIInfo();
            eiInfo.Set("service", "ZMCA23");
            eiInfo.Set("method", "queryInfoForPie");

            HttpServiceAgent agent = new HttpServiceAgent();
            agent.CallService(eiInfo, queryByPieCompleted);
        }

        private void queryByPieCompleted(object sender, DoWorkCompletedEventArgs e)
        {

            InitPieChart();
            EIInfo eiInfo = e.EIInfo;
            EIBlock resultBlock = eiInfo.GetBlock("result");

            //List<List<DataPoint>> itemsSource = new List<List<DataPoint>>();
            List<DataPoint> totalList = new List<DataPoint>();

            int departmentCount = 1;
            int totalForTop5Department = 0;
            int total = 0;


            if (resultBlock != null && resultBlock.Rows.Count > 0)
            {
                IList<EIRow> resultList = resultBlock.Rows;
                AddUpToBase(resultList);

                //降序排列，选择指定层级（LEVEL）的单位
                var sortedList = (from n in resultList
                                  where (Common.GetIntValue(n["fdDate"]) == ZMCA83Controler.LEVEL)
                                  orderby (Common.GetIntValue(n["fdKeyId"])) descending
                                  select n).ToList<EIRow>();



                foreach (var item in sortedList)
                {

                    string date = item["fdDepartmentId"].ToString();
                    if (date.Trim() == string.Empty)
                    {
                        continue;
                    }

                    DataPoint dp = new DataPoint();
                    dp.YValue = Common.GetIntValue(item["fdKeyId"]);

                    if (dp.YValue == 0)
                        continue;


                    totalForTop5Department += Common.GetIntValue(item["fdKeyId"]);
                    dp.XCategory = Common.GetStringValue(item["fdName"]);
                    dp.LegendLabel = Common.GetStringValue(item["fdName"]);

                    totalList.Add(dp);

                    departmentCount++;
                    if (departmentCount > 5)
                    {
                        break;
                    }
                }
                if (sortedList.Count > 5)
                {
                    DataPoint dp = new DataPoint();
                    for (int j = 5; j < sortedList.Count; j++)
                    {
                        dp.YValue += Common.GetIntValue(sortedList[j]["fdKeyId"]);
                    }

                    dp.XCategory = "其他";
                    dp.LegendLabel = "其他";
                    if(dp.YValue > 0) totalList.Add(dp);
                }

                //itemsSource.Add(totalList);
                //PieChart.SeriesMappings.Clear();
                PieChart.ItemsSource = null;
                PieChart.ItemsSource = totalList;
            }
        }



        #region 累计辅助函数（与ZMCA83内类似）


        private void AddUpToBase(IList<EIRow> list)
        {
            foreach (var item in list)       //累计运算
            {
                if (Common.GetIntValue(item["fdDate"]) == 1)    //只对LEVEL = 1的层级进行累加
                    AddUp(item, list);
            }
        }

        private void AddUp(EIRow item, IList<EIRow> list)
        {
            string fdDepartmentId = item["fdDepartmentId"].ToString().Trim();
            var childItems = (from element in list where (element["fdParentId"] != null) &&
                                  (element["fdParentId"].ToString().Trim().Equals(fdDepartmentId))
                              select element).ToList<EIRow>();

            foreach (var it in childItems)
            {
                AddUp(it, list);

                int itemValue = Common.GetIntValue(item["fdKeyId"]);
                int childValue = Common.GetIntValue(it["fdKeyId"]);
                item.Add("fdKeyId", itemValue + childValue);
            }
        }


        #endregion




    }
}